//
//  QJMineAddressVC.m
//  QuanJieShopProject
//
//  Created by 刘中义 on 2018/2/19.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJMineAddressVC.h"
#import "QJMineAddressCell.h"
#import "QJMineEditAddressVC.h"
@interface QJMineAddressVC () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *addressTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@end

@implementation QJMineAddressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupAddressUIAction];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(defaultAddressNotification) name:@"defaultAddressNotification" object:nil];
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(defaultAddressNotification) name:@"delAddressNotification" object:nil];
    
}
- (void)defaultAddressNotification{
    
    [self dataRequestAction];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavAlpha:YES];
    //控件不被导航栏遮挡
    self.navigationController.navigationBar.translucent = NO;
    //数据请求
    [self dataRequestAction];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self setNavAlpha:NO];
    self.navigationController.navigationBar.translucent = YES;
}
#pragma mark -- 界面布局
- (void)setupAddressUIAction{
    self.title = @"地址管理";
    self.addressTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT - 115) style:UITableViewStylePlain];
    [self.addressView addSubview:self.addressTableView];
    self.addressTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.addressTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.addressTableView.delegate = self;
    self.addressTableView.dataSource = self;
}
#pragma mark -- 数据请求
- (void)dataRequestAction{
    
    [WWNetRequest fetchAddressListWithParameters:nil success:^(id response) {
        
        NSLog(@"获取地址 response = %@", response);
        NSArray *resultArr = response[@"data"];
        [self.dataSourceArray removeAllObjects];
        NSMutableArray *tempArr = [NSMutableArray array];
        for (NSDictionary *dict in resultArr) {
            MineAddressModel *model = [[MineAddressModel alloc]initWithDictionary:dict error:nil];
            [tempArr addObject:model];
        }
        
        self.dataSourceArray = tempArr;
        [self.addressTableView reloadData];
        
    } failure:^(NSDictionary *error) {
        NSLog(@"获取地址 error = %@", error);

    }];
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cellID";
    QJMineAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"QJMineAddressCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    MineAddressModel *model = self.dataSourceArray[indexPath.row];
    [cell cellUpdateWithModel:model];
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MineAddressModel *model = self.dataSourceArray[indexPath.row];
    if ([self.typeStr isEqualToString:@"mine"]) {
        //从个人中心跳转过来的界面
       
    }else{
       
        if (self.getOneAddressBlock) {
            
             self.getOneAddressBlock(model);
        }
       
        [self.navigationController popViewControllerAnimated:YES];
        //从别的地方跳转过来的界面(直接走设为默认地址的方法)
//        NSString *address = [NSString stringWithoutNilFormart:model.address_id];
//        [self getOneAddressWithAddressID:address];
    }
    
}



#pragma mark -- 获取一条地址
- (void)getOneAddressWithAddressID:(NSString *)addressId{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:addressId forKey:@"addressId"];
    [WWNetRequest fetchOneAddressWithParameters:dict success:^(id response) {
        if (response) {
            if (self.getOneAddressBlock) {
                self.getOneAddressBlock(response);
            }
            KPopViewControllerAction;
        }
    } failure:^(NSDictionary *error) {
        
    }];
}
#pragma makr -- 添加地址
- (IBAction)addAddressBtnClickAction:(UIButton *)sender {
    QJMineEditAddressVC *VC = [[QJMineEditAddressVC alloc]init];
    KPushViewControllerAction(VC);
}

#pragma mark -- 懒加载
-(NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
